import { createRouter, createWebHistory } from 'vue-router'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    //重定向
    {path:'/', redirect:'/login'},
    // {path: '/home',  meta:{title:'主页'},component: () => import('../views/Home.vue'),},
      {path: '/login',  meta:{title:'登陆'},component: () => import('../views/Login.vue'),},
      {path: '/register',  meta:{title:'注册'},component: () => import('../views/Register.vue'),},
      {path: '/screen', meta:{title:'智慧大屏'},component: () => import('../views/Screen.vue'),},
    {path: '/camera', meta:{title:'监控'},component: () => import('../views/Camera.vue'),},


  ]
})
// meta 补充信息
// brforeEach 跳转之前做的一些操作
router.beforeEach((to,from,next)=>{
  document.title = to.meta.title

  const isLogged = JSON.parse(localStorage.getItem('token'))
  if (!isLogged) {
    if (to.path !== '/login' && to.path !== '/register') {
      next('/login'); // 未登录且尝试访问受限路由，重定向到登录页面2
    } else {
      next(); // 未登录但允许访问的路由，允许导航
    }
  } else {
    next(); // 已登录，允许访问所有路由,必须调用这个函数才能实现跳转
  }
})
export default router
